package com.shundr.shipper;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.shundr.shipper.base.BaseFragment;
import com.shundr.shipper.cargo.CargoTemplateActivity;
import com.shundr.shipper.cargo.DeliverCargoActivity;
import com.shundr.shipper.cargo.MyCargoListActivity;
import com.shundr.shipper.common.FavoriteListActivity;
import com.shundr.shipper.common.InvitationActivity;
import com.shundr.shipper.common.adapter.ViewPagerAdapter;
import com.shundr.shipper.common.view.DialogCall;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.order.OrderListActivity;
import com.shundr.shipper.truck.FamTruckListActivity;
import com.shundr.shipper.user.CreditsActivity;
import com.shundr.shipper.user.LoginActivity;
import com.umeng.soexample.widget.CustomShareBoard;

/**
 * 首页
 * MainNewFragment
 * 
 * @author larry 2015-4-13 上午10:50:55 
 * @version 1.0.0
 *
 */
public class MainNewFragment extends BaseFragment implements OnClickListener, OnPageChangeListener {

	private View mView;
	
	private RelativeLayout mLayoutPager;
	
	private ViewPager viewPager;
	private ViewPagerAdapter vpAdapter;
	private ArrayList<ImageView> views;
	private int[] pics = { R.drawable.bg_main_splash_1, R.drawable.bg_main_splash_2,
			R.drawable.bg_main_splash_3 };
	private ImageView[] points;
	private int currentIndex;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 100:
				new Timer().schedule(new TimerTask() {

					@Override
					public void run() {
						mHandler.sendEmptyMessage(101);
					}
				}, 5000, 5000);
				break;
			case 101:
				int currentItem = viewPager.getCurrentItem();
				if (currentItem == 2) {
					viewPager.setCurrentItem(0, false);
				} else {
					viewPager.setCurrentItem(currentItem + 1, false);
				}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		mView = inflater.inflate(R.layout.fragment_main_grid, null);//
		initView(mView);
		return mView;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}
	
	private void initView(View view) {
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		LContext.width = dm.widthPixels; // 当前屏幕像素
		LContext.height = dm.heightPixels; // 当前屏幕像素
		
		mLayoutPager = (RelativeLayout)view.findViewById(R.id.layout_viewpager);
		LayoutParams params = (LayoutParams)mLayoutPager.getLayoutParams();
		params.height = (int)(LContext.height * 0.3);
		
		views = new ArrayList<ImageView>();
		viewPager = (ViewPager) view.findViewById(R.id.viewpager);
		vpAdapter = new ViewPagerAdapter(views);
		
		view.findViewById(R.id.layout_main_cargo).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(LContext.isLogin()){
					startActivity(new Intent(mContext, MyCargoListActivity.class));
				}else{
					startActivity(new Intent(mContext, LoginActivity.class));
				}
			}
		});
		
		view.findViewById(R.id.layout_main_fam_truck).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (LContext.isLogin()) {
							startActivity(new Intent(mContext,FamTruckListActivity.class));
						} else {
							startActivity(new Intent(mContext,LoginActivity.class));
						}
					}
				});
		
		view.findViewById(R.id.layout_main_template).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if(LContext.isLogin()){
							startActivity(new Intent(mContext, CargoTemplateActivity.class));
						}else{
							startActivity(new Intent(mContext, LoginActivity.class));
						}
					}
				});
		view.findViewById(R.id.layout_main_favorite).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(LContext.isLogin()){
					startActivity(new Intent(mContext, FavoriteListActivity.class));
				}else{
					startActivity(new Intent(mContext, LoginActivity.class));
				}
			}
		});
		view.findViewById(R.id.layout_main_wallet).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//积分查询
				if(LContext.isLogin()){
					Intent intent = new Intent(mContext, CreditsActivity.class);
					startActivity(intent);
				}else{
					startActivityForResult(new Intent(getActivity(), LoginActivity.class), 500);
				}
			}
		});

		view.findViewById(R.id.layout_main_service).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						DialogCall callDialog = new DialogCall(mContext);
						callDialog.show();
					}
				});
		view.findViewById(R.id.layout_main_share).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						CustomShareBoard shareBoard = new CustomShareBoard(getActivity());
				        shareBoard.showAtLocation(getActivity().getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
					}
				});
		view.findViewById(R.id.layout_main_invitation).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//邀请
				if(LContext.isLogin()){
					Intent intent = new Intent(mContext, InvitationActivity.class);
					startActivity(intent);
				}else{
					startActivityForResult(new Intent(getActivity(), LoginActivity.class), 500);
				}
			}
		});
		
		view.findViewById(R.id.btn_publish_cargo).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(LContext.isLogin()){
					startActivity(new Intent(mContext, DeliverCargoActivity.class));
				}else{
					startActivity(new Intent(mContext, LoginActivity.class));
				}
			}
		});
		
		view.findViewById(R.id.btn_publish_car).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(LContext.isLogin()){
					startActivity(new Intent(mContext, OrderListActivity.class));
				}else{
					startActivity(new Intent(mContext, LoginActivity.class));
				}
			}
		});
	}

	private void initData() {
		viewPager.setAdapter(vpAdapter);
		viewPager.setOnPageChangeListener(this);
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.MATCH_PARENT);

				for (int i = 0; i < pics.length; i++) {
					ImageView iv = new ImageView(mContext);
					iv.setLayoutParams(mParams);
					// iv.setScaleType(ScaleType.FIT_XY);
					iv.setBackgroundResource(pics[i]);
					views.add(iv);
				}

				vpAdapter.notifyDataSetChanged();
				initPoint(mView);
				mHandler.sendEmptyMessage(100);
			}
		}, 0);

	}

	private void initPoint(View view) {
		LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_bottom);
		points = new ImageView[pics.length];
		for (int i = 0; i < pics.length; i++) {
			points[i] = (ImageView) linearLayout.getChildAt(i);
			points[i].setEnabled(true);
			points[i].setOnClickListener(this);
			points[i].setTag(i);
		}
		currentIndex = 0;
		points[currentIndex].setEnabled(false);
	}
	
	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		//mMapManager.resume();
		super.onResume();
	}

	@Override
	public void onDestroy() {
		mHandler.removeCallbacksAndMessages(null);
		for (ImageView view : views) {
			releaseImageView(view);
		}
		super.onDestroy();
	}
	
	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		setCurDot(arg0);
	}

	@Override
	public void onClick(View v) {
		int position = (Integer) v.getTag();
		setCurView(position);
		setCurDot(position);
	}

	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}
		viewPager.setCurrentItem(position);
	}

	private void setCurDot(int positon) {
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}
		points[positon].setEnabled(false);
		points[currentIndex].setEnabled(true);

		currentIndex = positon;
	}

}
